package com.jy.oms.framework.asyn.task.constants;


import com.jy.oms.framework.asyn.task.core.ExecutorConfig;
import com.jy.oms.framework.asyn.task.core.ExecutorContext;
import org.apache.commons.lang3.StringUtils;

/**
 * 缓存常量
 *
 * @author liuquan
 */
public interface CacheConstants {
    //默认缓存前缀
    String DEFAULT_CACHE_PREFIX = "asyn-task";
    //分隔符
    String KEY_SPLIT = ":";
    //job/task工作服务器IP
    String WORKER_SERVER_RUN = "ASYN_TASK:WORKER_SERVER_RUN";
    //工作服务器校验key
    String WORKER_SERVER_RUN_JUDGE = "ASYN_TASK:WORKER_SERVER_RUN_JUDGE";
    //任务队列key
    String TASK_QUEUE = "ASYN_TASK:TASK_QUEUE";
    //限速器key
    String REQUEST_RATE_LIMITER = "ASYN_TASK:REQUEST_RATE_LIMITER";
    //任务是否已执行
    String TASK_EXECUTED_CHECKER = "ASYN_TASK:TASK_EXECUTED_CHECKER";
    //排他任务是否在执行
    String TASK_EXCLUSIVE_EXECUTE_CHECKER = "ASYN_TASK:TASK_EXCLUSIVE_EXECUTE_CHECKER";
    //相同业务单号排他任务是否在执行
    String TASK_EXCLUSIVE_SAME_BILL_EXECUTE_CHECKER = "ASYN_TASK:TASK_EXCLUSIVE_SAME_BILL_EXECUTE_CHECKER";
    //DB队列任务统计
    String TASK_STATISTIC = "ASYN_TASK:TASK_STATISTIC";
    //相同的任务参数只会存在一条任务
    String UNIQUE_TASK_ACCOARD_PARAM_LOCK = "ASYN_TASK:UNIQUE_TASK_ACCOARD_PARAM_LOCK";

    //任务注册节点信息
    String TASK_REGISTRY_NODE_UPDATOR_MAP = "ASYN_TASK:TASK_REGISTRY_NODE_UPDATOR_MAP";
    //任务抽取查询时间
    String TASK_EXTRACTOR_QUERY_TIME_MAP = "ASYN_TASK:TASK_EXTRACTOR_QUERY_TIME_MAP";

    //超时任务时间比较key
    String TIMEOUT_TASK_TIME_COMPARE = "ASYN_TASK:TIMEOUT_TASK_TIME_COMPARE";
    //超时任务收集器key
    String TIMEOUT_TASK_COLLECTOR = "ASYN_TASK:TIMEOUT_TASK_COLLECTOR";

    //框架缓存前缀
    String FRAMEWORK_CACHE_PREFIX = "asyn-task-framework";

    //DB队列队列名与代理统计key映射关系
    String QUEUE_STATISTIC_CACHE_MAPPING = "QUEUE_BUSINESS_NAME_MAPPING";

    /**
     * 配置事件queue
     */
    String TASK_CONFIG_PUBLISHER_QUEUE = "ASYN_TASK:TASK_CONFIG_PUBLISHER_QUEUE";

    /**
     * 可执行节点服务ip注册
     */
    String EXECUTOR_SERVER_REGIST = "EXECUTOR_SERVER_REGIST";

    /**
     * 构造缓存键
     */
    static String genCacheKey(String key, Object... params) {
        ExecutorConfig executorConfig = ExecutorContext.getExecutorConfig();
        StringBuilder builder = new StringBuilder();
        builder.append(executorConfig.getBusinessGroupRedisPrefix())
                .append(KEY_SPLIT)
                .append(executorConfig.getBusinessGroup())
                .append(KEY_SPLIT)
                .append(executorConfig.getEnv())
                .append(KEY_SPLIT)
                .append(key);
        if (params.length > 0) {
            for (Object param : params) {
                String strParam = param.toString();
                if (StringUtils.isNotBlank(strParam)) {
                    builder.append(KEY_SPLIT).append(strParam);
                }
            }
        }
        return builder.toString();
    }

    static String genFrameworkKey(String key, Object... params) {
        StringBuilder builder = new StringBuilder(FRAMEWORK_CACHE_PREFIX);
        if(StringUtils.isNotBlank(key)){
            builder.append(KEY_SPLIT).append(key);
        }
        if (params.length > 0) {
            for (Object param : params) {
                String strParam = param.toString();
                if (StringUtils.isNotBlank(strParam)) {
                    builder.append(KEY_SPLIT).append(strParam);
                }
            }
        }
        return builder.toString();
    }
}
